/*
 * DestinationScreen.java
 *
 */
package com.gpsnav.screen;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.component.AutoTextEditField;
import javax.microedition.location.Coordinates;
import net.rim.device.api.ui.text.NumericTextFilter;
import net.rim.device.api.util.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import java.io.IOException;
import com.gpsnav.bo.Options;
import com.gpsnav.dao.OptionsDAO;

import javax.microedition.location.*;

public class OptionsScreen extends MainScreen {

    RadioButtonGroup coordinateGroup = new RadioButtonGroup();
    RadioButtonGroup directionGroup = new RadioButtonGroup();
    RadioButtonGroup distanceGroup = new RadioButtonGroup();
    RadioButtonGroup speedGroup = new RadioButtonGroup();
    Options options = new Options();
    OptionsDAO oDao = new OptionsDAO();

    public OptionsScreen() {
        options = oDao.loadOptions();
        
        HorizontalFieldManager optionsHZM = new HorizontalFieldManager();
        VerticalFieldManager optionsVFM = new VerticalFieldManager();
        optionsHZM.add(new LabelField("\nCoordinate Format:  "));
       
        for (int i = 0;i<options.getCoordList().length;i++) {
            boolean selected = (options.getCoordFormat() == i);
            RadioButtonField coord = new RadioButtonField(options.getCoordList()[i], coordinateGroup, selected);
            optionsVFM.add(coord);
        }
        optionsHZM.add(optionsVFM);
        add(optionsHZM);
        add(new SeparatorField());
        
        HorizontalFieldManager distanceHZM = new HorizontalFieldManager();
        VerticalFieldManager distanceVFM = new VerticalFieldManager();
        distanceHZM.add(new LabelField("Distance Format:  "));
        
        for (int i = 0;i< options.getDistanceList().length;i++) {
            boolean selected = (options.getDistanceFormat() == i);
            RadioButtonField f = new RadioButtonField(options.getDistanceList()[i], distanceGroup, selected);
            distanceVFM.add(f);
        }
        distanceHZM.add(distanceVFM);
        add(distanceHZM);
        add(new SeparatorField());
        
        HorizontalFieldManager directionHZM = new HorizontalFieldManager();
        VerticalFieldManager directionVFM = new VerticalFieldManager();
        directionHZM.add(new LabelField("\nDirection Format:  "));
        
        for (int i = 0;i< options.getDirectionList().length;i++) {
            boolean selected = (options.getDirectionFormat() == i);
            RadioButtonField f = new RadioButtonField(options.getDirectionList()[i], directionGroup, selected);
            directionVFM.add(f);
        }
        directionHZM.add(directionVFM);
        add(directionHZM);
        add(new SeparatorField());
        
        HorizontalFieldManager speedHZM = new HorizontalFieldManager();
        VerticalFieldManager speedVFM = new VerticalFieldManager();
        speedHZM.add(new LabelField("\nSpeed Format:  "));
        
        for (int i = 0;i< options.getSpeedList().length;i++) {
            boolean selected = (options.getSpeedFormat() == i);
            RadioButtonField f = new RadioButtonField(options.getSpeedList()[i], speedGroup, selected);
            speedVFM.add(f);
        }
        speedHZM.add(speedVFM);
        add(speedHZM);
        
        

    }

    public MenuItem saveMenuItem = new MenuItem("Save Options", 1,1){
        public void run(){
            save();
            UiApplication.getUiApplication().popScreen();
        }
        
    };

    public void save(){
            options.setCoordFormat(coordinateGroup.getSelectedIndex());
            options.setDistanceFormat(distanceGroup.getSelectedIndex());
            options.setDirectionFormat(directionGroup.getSelectedIndex());
            options.setSpeedFormat(speedGroup.getSelectedIndex());
            oDao.storeOptions(options);
        }


    public MenuItem aboutMenuItem = new MenuItem("About", 3,3){
        public void run(){
            Dialog.inform("This application has been designed for non-commercial use. Any queries please contact me via paul.haenel@gmail.com!");
        }
    };

    public MenuItem exitMenuItem = new MenuItem("Exit", 4,4){
        public void run(){ System.exit(1); }
    };

    protected void makeMenu(Menu menu, int instance){
        menu.add(saveMenuItem);
        menu.add(aboutMenuItem);
        menu.add(exitMenuItem);
    }
    
    public void setOptions(Options o) {
        options = o;
    }
    
    public Options getOptions () {
        return options;
    }
}

